export default {
	init:(Vue)=>{
		/**
		 * 定义请求域名 
		 */
		Vue.prototype.base_url = "https://api.solrswap.online/api/";
		/**
		 * 静态资源url
		 */
		Vue.prototype.static_url = "http://btmg.kkanshan.com/hscb/img/";
		/**
		 * 当前项目的登录页 url
		 */
		Vue.prototype.login_page_path = "/pages/login/login";
		
		/**
		 * 定义请求成功状态码 
		 */
		Vue.prototype.succ_code = "200";
		/**
		 * 定义未登录状态码
		 */
		
		Vue.prototype.login_fail_code = "1001";
		/**
		 * @param {Object} url 请求路径
		 * @param {Object} param 请求参数 {}
		 * @param {Object} success 请求成功后的回调
		 * @param {Object} fail	请求失败后的回调
		 * @param {Object} complete 请求完成后的回调
		 */
		Vue.prototype.req_post = function(url, param, success, fail, complete) {
			try {
				if(url.indexOf('http') == -1){
					url = Vue.prototype.base_url + url;
				}
				let header = {
				};
				let token = uni.getStorageSync("token");
				if (token) {
					header = {
						"Authorization": "Bearer " + token,
					};
				}
				uni.request({
					url: url,
					method: "POST",
					header: header,
					data: param,
					success(result) {
						let super_result = result;
						result = result.data;
						// console.log("APP",result)
						if (typeof(success) == "function") {
							if (result.code == Vue.prototype.login_fail_code) {
								//如果接口提示没有登录，则清空本地缓存
								Vue.prototype.clear_login_cache();
							}
							success(result);
						}
					},
					fail(result) {
						if (typeof(fail) == "function") {
							fail(result);
						}
					},
					complete(result) {
						// uni.hideLoading();
						if (typeof(complete) == "function") {
							complete(result);
						}
					}
				})
			} catch (error) {
				uni.showModal({
					content:"程序发生错误:"+JSON.stringify(error)
				})
			}
		}
		/**
		 * @param {Object} url 请求路径
		 * @param {Object} param 请求参数 {}
		 * @param {Object} success 请求成功后的回调
		 * @param {Object} fail	请求失败后的回调
		 * @param {Object} complete 请求完成后的回调
		 */
		Vue.prototype.post = function(url, param, success, fail, complete) {
			Vue.prototype.req_post(url, param, success, fail, complete);
		}
		Vue.prototype.get = function(url, param, success, fail, complete) {
			Vue.prototype.req_get(url, param, success, fail, complete);
		}
		Vue.prototype.auth_address = function(callback) {
			Vue.prototype.req_post("/auth/address",{},res=>{
				if(res.code == 200){
					if(typeof callback == "function"){
						callback(res.data.address)
					}
				}else{
					if(typeof callback == "function"){
						callback(null)
					}
				}
			});
		}
		/**
		 * @param {Object} url 请求路径
		 * @param {Object} param 请求参数 {}
		 * @param {Object} success 请求成功后的回调
		 * @param {Object} fail	请求失败后的回调
		 * @param {Object} complete 请求完成后的回调
		 */
		Vue.prototype.req_get = function(url, param, success, fail, complete) {
			try {
				url = Vue.prototype.base_url + url;
				let header = {
					"Content-Type": "application/json; charset=utf-8",
					"XX-Device-Type": "mobile",
					"platform": "MP-WEIXIN"
				};
				let token = uni.getStorageSync("token");
				if (token) {
					header = {
						"Content-Type": "application/json; charset=utf-8",
						"Authorization": "Bearer " + token,
						"XX-Device-Type": "mobile",
						"platform": "MP-WEIXIN"
					};
				}
				uni.request({
					url: url,
					method: "GET",
					header: header,
					data: param,
					success(result) {
						// console.log("APP",result)
						if (typeof(success) == "function") {
							result = result.data;
							if (result.code == Vue.prototype.login_fail_code) {
								//如果接口提示没有登录，则清空本地缓存
								Vue.prototype.clear_login_cache();
							}
							success(result);
						}
					},
					fail(result) {
						if (typeof(fail) == "function") {
							fail(result);
						}
					},
					complete(result) {
						if (typeof(complete) == "function") {
							complete(result);
						}
					}
				})
			} catch (error) {
				console.log(error, 2)
			}
		}
		
		
		
	}
}